package com.ruoyi.system.controller;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.AddForm;
import com.ruoyi.system.domain.Users;
import com.ruoyi.system.mapper.FocusMapper;
import com.ruoyi.system.service.FocusService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("focus")
public class FocusController extends BaseController {

    @Autowired
    FocusService focusService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("userlist")
    public R userlist(){
        List<Users> list = focusService.queryAllUser();
        return R.ok(list);
    }

    @GetMapping("getprivateFacilityOpt")
    public R getprivateFacilityOpt(){
        List<Users> list = focusService.getprivateFacilityOpt();
        return R.ok(list);
    }

    @PreAuthorize(hasPermi = "system:focus:addForm")
    @PostMapping("/addForm")
    public int addForm(@RequestBody AddForm addForm){
        int i=0;
        Snowflake snowflake = new Snowflake(1,1);
        addForm.setHouseId(snowflake.nextIdStr());
        addForm.setId(snowflake.nextIdStr());
        addForm.setRoomId(snowflake.nextIdStr());
        Boolean b = redisTemplate.opsForValue().setIfAbsent(addForm.toString(), "addRoom", 5, TimeUnit.MINUTES);
        if(b){
            try {
                focusService.addRoomInfo(addForm);
                i= focusService.addHouseInfo(addForm);
                rabbitTemplate.convertAndSend("PublicExchange", "", JSON.toJSONString(addForm));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return i;
    }

}
